package com.gaga.metadata.query;

import com.gaga.metadata.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * AbstractDBQuery
 *
 * @author liujia
 * @since 2021/7/20 13:50
 **/
public abstract class AbstractDBQuery implements IDBQuery {

    @Override
    public String createTableSql(String schemaName, String tableName) {
        return "show create table " + schemaName + "." + tableName;
    }

    @Override
    public String createTableName() {
        return "Create Table";
    }

    @Override
    public String[] columnCustom() {
        return null;
    }

    @Override
    public String schemaName() {
        return "SCHEMA";
    }

    @Override
    public String catalogName() {
        return "CATALOG";
    }

    @Override
    public String tableName() {
        return "NAME";
    }

    @Override
    public String tableComment() {
        return "COMMENT";
    }

    @Override
    public String tableType() {
        return "TYPE";
    }

    @Override
    public String engine() {
        return "ENGINE";
    }

    @Override
    public String options() {
        return "OPTIONS";
    }

    @Override
    public String rows() {
        return "ROWS";
    }

    @Override
    public String createTime() {
        return "CREATE_TIME";
    }

    @Override
    public String updateTime() {
        return "UPDATE_TIME";
    }

    @Override
    public String columnName() {
        return "COLUMN_NAME";
    }

    @Override
    public String columnPosition() {
        return "ORDINAL_POSITION";
    }

    @Override
    public String columnType() {
        return "DATA_TYPE";
    }

    @Override
    public String columnComment() {
        return "COLUMN_COMMENT";
    }

    @Override
    public String columnKey() {
        return "COLUMN_KEY";
    }

    @Override
    public String autoIncrement() {
        return "AUTO_INCREMENT";
    }

    @Override
    public String defaultValue() {
        return "COLUMN_DEFAULT";
    }

    @Override
    public String isNullable() {
        return "NULL";
    }


    @Override
    public String length() {
        return "LENGTH";
    }

    @Override
    public String precision() {
        return "NUMERIC_PRECISION";
    }

    @Override
    public String dateTimePrecision() {
        return "DATETIME_PRECISION";
    }

    @Override
    public String scale() {
        return "NUMERIC_SCALE";
    }

    @Override
    public String characterSet() {
        return "CHARACTER_SET_NAME";
    }

    @Override
    public String collation() {
        return "COLLATION_NAME";
    }

    @Override
    public String isPK() {
        return "PRI";
    }

    @Override
    public String limit(Integer pageNum, Integer pageSize) {
        return "limit " + (pageNum - 1) * pageSize + " " + pageSize;
    }

    @Override
    public String createBatchInsertSql(String schemaName, String tableName, List<String> columns, List<Map<Integer, String>> dataList) {
        StringBuilder sql = new StringBuilder()
                .append("insert into ");

        if (StringUtils.isNotBlank(schemaName)) {
            sql.append(schemaName)
                    .append(".");
        }
        sql.append(tableName)
                .append("(");

        int nameCount = 0;
        for (String column : columns) {
            if (nameCount > 0) {
                sql.append(",");
            }
            sql.append(column);
            nameCount++;
        }
        sql.append(") values ");
        for (int i = 0; i < dataList.size(); i++) {
            sql.append("(");
            for (int j = 0; j < nameCount; ++j) {
                if (j != 0) {
                    sql.append(",");
                }
                sql.append("?");
            }
            if (i + 1 == dataList.size()) {
                sql.append(");");
            } else {
                sql.append("),");
            }
        }

        return sql.toString();
    }

}
